/**
 * @author مۇختەرجان مەخمۇت (mutearjan mahmood)
 * @email yeganaaa@163.com
 * @create date 2020-10-06 11:54:35
 * @modify date 2020-10-06 11:54:35
 * @desc [description]
 */
import {IRpcBridgeHost, IRpcServerHost, ServerHostEvents} from "../Exports";

export class CustomizableServerHost implements IRpcServerHost<IRpcServerHost<any>> {
    constructor(public backend: IRpcServerHost<any>, public onClientConnected: (bridge: IRpcBridgeHost<any>) => Promise<IRpcBridgeHost<any>> = async bridge => bridge) {}
    public async prepare(eventHandler: ServerHostEvents): Promise<void> {
        await this.backend.prepare(new ServerHostEvents(
            eventHandler.onReady,
            async bridge => eventHandler.onClientConnected(await this.onClientConnected(bridge)),
            eventHandler.onError,
            eventHandler.onShutdown
        ))
    }
    public async launch(): Promise<void> {
        await this.backend.launch()
    }
    public async shutdown(): Promise<void> {
        await this.backend.shutdown()
    }
}
